/**
 * gwt-validator 
 *
 * Copyright (C) 2006  Nikolaus Rumm, Christian Simonutti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package gwt.validator.client;

import gwt.validator.client.action.Action;
import gwt.validator.client.action.AddMessageAction;
import gwt.validator.client.action.ConditionalInvokerAction;
import gwt.validator.client.action.FirstInvocationCondition;
import gwt.validator.client.action.SetFocusAction;
import gwt.validator.client.action.SetMessageAction;
import gwt.validator.client.check.Check;
import gwt.validator.client.check.IntegerMaxValueCheck;
import gwt.validator.client.check.IntegerMinValueCheck;
import gwt.validator.client.check.IntegerRangeCheck;
import gwt.validator.client.check.MandatoryCheck;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Panel for testing the Integer validators.
 * 
 * @author <a href="mailto:nikolaus.rumm@gmail.com">Nikolaus Rumm</a>
 */
public class IntegerTestPanel extends VerticalPanel {

	private Label mandatoryValidatorLabel;
	private TextBox mandatoryValidatorText;
	private Label mandatoryValidatorErrorLabel;

	private Label minValueValidatorLabel;
	private TextBox minValueValidatorText;
	private Label minValueValidatorErrorLabel;

	private Label maxValueValidatorLabel;
	private TextBox maxValueValidatorText;
	private Label maxValueValidatorErrorLabel;

	private Label rangeValidatorLabel;
	private TextBox rangeValidatorText;
	private Label rangeValidatorErrorLabel;

	private Label radioLabel;
	private RadioButton radio1;
	private RadioButton radio2;
	private RadioButton radio3;
	private Label radioErrorLabel;

	private Label errorLabel;

	/**
	 * Constructor
	 */
	public IntegerTestPanel() {
		this.init();
		this.setWidth("100%");
		this.setHeight("100%");
	}

	/**
	 * Initializes the panel's widgets
	 */
	public void init() {
		this.mandatoryValidatorLabel = new Label("Mandatory");
		this.mandatoryValidatorText = new TextBox();
		this.mandatoryValidatorErrorLabel = new Label();
		this.mandatoryValidatorErrorLabel.setStyleName("error");

		this.rangeValidatorLabel = new Label("Range validator");
		this.rangeValidatorText = new TextBox();
		this.rangeValidatorErrorLabel = new Label();
		this.rangeValidatorErrorLabel.setStyleName("error");

		this.minValueValidatorLabel = new Label("Min value validator");
		this.minValueValidatorText = new TextBox();
		this.minValueValidatorErrorLabel = new Label();
		this.minValueValidatorErrorLabel.setStyleName("error");

		this.maxValueValidatorLabel = new Label("Max value validator");
		this.maxValueValidatorText = new TextBox();
		this.maxValueValidatorErrorLabel = new Label();
		this.maxValueValidatorErrorLabel.setStyleName("error");

		this.radioLabel = new Label("Radio button value");
		this.radio1 = new RadioButton("radioGroup", "One");
		this.radio2 = new RadioButton("radioGroup", "Two");
		this.radio3 = new RadioButton("radioGroup", "Three");
		this.radioErrorLabel = new Label();
		this.radioErrorLabel.setStyleName("error");
		FlowPanel radioPanel = new FlowPanel();
		radioPanel.add(this.radio1);
		radioPanel.add(this.radio2);
		radioPanel.add(this.radio3);

		Grid g = new Grid(5, 3);
		g.setWidget(0, 0, this.mandatoryValidatorLabel);
		g.setWidget(0, 1, this.mandatoryValidatorText);
		g.setWidget(0, 2, this.mandatoryValidatorErrorLabel);
		g.setWidget(1, 0, this.rangeValidatorLabel);
		g.setWidget(1, 1, this.rangeValidatorText);
		g.setWidget(1, 2, this.rangeValidatorErrorLabel);
		g.setWidget(2, 0, this.minValueValidatorLabel);
		g.setWidget(2, 1, this.minValueValidatorText);
		g.setWidget(2, 2, this.minValueValidatorErrorLabel);
		g.setWidget(3, 0, this.maxValueValidatorLabel);
		g.setWidget(3, 1, this.maxValueValidatorText);
		g.setWidget(3, 2, this.maxValueValidatorErrorLabel);
		g.setWidget(4, 0, this.radioLabel);
		g.setWidget(4, 1, radioPanel);
		g.setWidget(4, 2, this.radioErrorLabel);

		this.add(g);

		Button b = new Button("Validate");
		b.addClickListener(new ClickListener() {

			public void onClick(Widget aSender) {
				validate();
			}

		});
		this.add(b);

		this.errorLabel = new Label();
		this.errorLabel.setStyleName("error");
		this.add(errorLabel);
	}

	public void validate() {
		this.errorLabel.setText("");

		// this is more or less a mutable Boolean...
		FirstInvocationCondition scopeCondition = new FirstInvocationCondition();

		// determine the current radio button value
		Integer radioValue = null;
		if (this.radio1.isChecked()) {
			radioValue = new Integer(1);
		} else if (this.radio2.isChecked()) {
			radioValue = new Integer(2);
		} else if (this.radio3.isChecked()) {
			radioValue = new Integer(3);
		} else {
			radioValue = new Integer(0);
		}

		new Validator(
				new ValidationScope[] {
						new ValidationScope(new Check[] { new MandatoryCheck(
								this.mandatoryValidatorText,
								this.mandatoryValidatorLabel) }, new Action[] {
								new SetMessageAction(
										this.mandatoryValidatorErrorLabel),
								new AddMessageAction(this.errorLabel),
								new ConditionalInvokerAction(
										new SetFocusAction(
												this.mandatoryValidatorText),
										scopeCondition) }),
						new ValidationScope(
								new Check[] { new IntegerRangeCheck(
										this.rangeValidatorText,
										this.rangeValidatorLabel, 5, 10) },
								new Action[] {
										new SetMessageAction(
												this.rangeValidatorErrorLabel),
										new AddMessageAction(this.errorLabel),
										new ConditionalInvokerAction(
												new SetFocusAction(
														this.rangeValidatorText),
												scopeCondition) }),
						new ValidationScope(
								new Check[] { new IntegerMinValueCheck(
										this.minValueValidatorText,
										this.minValueValidatorLabel, 3) },
								new Action[] {
										new SetMessageAction(
												this.minValueValidatorErrorLabel),
										new AddMessageAction(this.errorLabel),
										new ConditionalInvokerAction(
												new SetFocusAction(
														this.minValueValidatorText),
												scopeCondition) }),
						new ValidationScope(
								new Check[] { new IntegerMaxValueCheck(
										this.maxValueValidatorText,
										this.maxValueValidatorLabel, 3) },
								new Action[] {
										new SetMessageAction(
												this.maxValueValidatorErrorLabel),
										new AddMessageAction(this.errorLabel),
										new ConditionalInvokerAction(
												new SetFocusAction(
														this.maxValueValidatorText),
												scopeCondition) }),
						new ValidationScope(
								new Check[] { new IntegerRangeCheck(radioValue,
										this.radioLabel, 2, 2) },
								new Action[] {
										new SetMessageAction(
												this.radioErrorLabel),
										new AddMessageAction(this.errorLabel),
										new ConditionalInvokerAction(
												new SetFocusAction(this.radio1),
												scopeCondition) }) })
				.validate();
	}
}
